package ru.rchervot.sanasar.client.view;

import java.util.ArrayList;
import java.util.Set;

import ru.rchervot.sanasar.client.model.Query;

import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public abstract class AbstractFiltersPanelView extends Composite
implements AbstractFiltersPanelPresenter.Display {
		
	private Presenter presenter;
	
	private final ListBox addFilter = new ListBox();
	private final ListBox orderBy = new ListBox();
	private final Label filtersLabel = new Label("Add filter");
	private final Label orderByLabel = new Label("Order by");
	private final Button resetButton = new Button("New Search");
	private final Button closeButton = new Button("Close");
	private final Button searchButton = new Button("Search");
	private final Label noFilters = new Label("No filters added.");
	private final VerticalPanel basePanel = new VerticalPanel();
	private final ScrollPanel scrollPanel = new ScrollPanel(); 
	private final ListBox queryJoin = new ListBox();
	private final Label queryJoinLabel = new Label("Match");
	
	private final VerticalPanel filtersPanel = new VerticalPanel();
	
	private final ArrayList<String> allFiltersNames;
	private final ArrayList<String> allFiltersNamesHuman;
	
	public AbstractFiltersPanelView(ArrayList<String> allFiltersNames, 
			ArrayList<String> allFiltersNamesHuman,
			ArrayList<String> orderByItems, ArrayList<String> matchItems, 
			ArrayList<AbstractFilterView> filtersViews) {
		super();
		
		this.allFiltersNames = allFiltersNames;
		this.allFiltersNamesHuman = allFiltersNamesHuman;
		
		for (AbstractFilterView filterView : filtersViews) {
			filtersPanel.add(filterView);
		}
		
		for (String item : orderByItems) {
			orderBy.addItem(item);
		}
		
//		for (String item : allFiltersNames) {
//			addFilter.addItem(item);
//		}
		
		for (String item : matchItems) {
			queryJoin.addItem(item);
		}
		
		scrollPanel.setWidth("100%");
		scrollPanel.setHeight("300px");
		
		noFilters.setWidth("100%");
		noFilters.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		HorizontalPanel addFilterPanel = new HorizontalPanel();
		addFilterPanel.setWidth("100%");
		addFilterPanel.setStylePrimaryName("AbstractFiltersView_pAddFilter");
		addFilterPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		addFilterPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		HorizontalPanel addFilterPanelR = new HorizontalPanel();	
		addFilterPanelR.setStylePrimaryName("AbstractFiltersView_pAddFilterR");
		addFilterPanelR.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		addFilterPanelR.add(filtersLabel);
		addFilterPanelR.add(addFilter); 
		
		HorizontalPanel addFilterPanelL = new HorizontalPanel();
		addFilterPanelL.setStylePrimaryName("AbstractFiltersView_pAddFilterL");
		addFilterPanelL.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		addFilterPanelL.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		resetButton.setEnabled(false);
		addFilterPanelL.add(resetButton);
		
		addFilterPanel.add(addFilterPanelL);
		addFilterPanel.add(addFilterPanelR);
		addFilterPanel.setCellHorizontalAlignment(addFilterPanelL, HasHorizontalAlignment.ALIGN_LEFT);
		addFilterPanel.setCellHorizontalAlignment(addFilterPanelR, HasHorizontalAlignment.ALIGN_RIGHT);
		
		
		HorizontalPanel queryPropertiesPanel = new HorizontalPanel();
		queryPropertiesPanel.setStylePrimaryName("AbstractFiltersView_pQuery");
		queryPropertiesPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		queryPropertiesPanel.add(orderByLabel);
		queryPropertiesPanel.add(orderBy);
		queryPropertiesPanel.add(queryJoinLabel);
		queryPropertiesPanel.add(queryJoin);
		

		HorizontalPanel actionsPanelRight = new HorizontalPanel();
		actionsPanelRight.setStylePrimaryName("AbstractFiltersView_pActionsR");
		actionsPanelRight.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		
		actionsPanelRight.add(closeButton);
		actionsPanelRight.add(searchButton);
		
		HorizontalPanel actionsPanel = new HorizontalPanel();
		actionsPanel.setWidth("100%");
		actionsPanel.setStylePrimaryName("AbstractFiltersView_pActions");
		
		actionsPanel.add(actionsPanelRight);
		
		actionsPanel.setCellHorizontalAlignment(actionsPanelRight, HasHorizontalAlignment.ALIGN_RIGHT);
		actionsPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		VerticalPanel bottomPanel = new VerticalPanel();
		bottomPanel.setWidth("100%");
		bottomPanel.setStylePrimaryName("AbstractFiltersView_pBottom");
		
		bottomPanel.add(queryPropertiesPanel);
		bottomPanel.add(actionsPanel);
		
		
		basePanel.add(addFilterPanel);
		basePanel.add(scrollPanel);
		basePanel.add(bottomPanel);
		
		basePanel.setWidth("700px");
		
		
		filtersPanel.setWidth("100%");
		
		
		scrollPanel.add(filtersPanel);
		
//		QueryWidget qw = createQueryWidget();
		
//		scrollPanel.add(qw);
//		scrollPanel.add(new AbstractFilterView("Full text search", new TextBox()));
		
		addFilter.setStylePrimaryName("AbstractFiltersView_addFilterList");
		
		this.initWidget(basePanel);
	}
	
	
	@Override
	public void renderQuery(Query query) {
		query.getOrderBy();
		query.getJoinOperator();
		query.getFiltersNames();
		
		refreshAddFilterList(query.getFiltersNames());
	}
	
	private void refreshAddFilterList(Set<String> queryFiltersNames) {
		addFilter.clear();
		
		for (int i = 0; i < allFiltersNames.size(); i++) {
			if (! queryFiltersNames.contains(allFiltersNames.get(i))) {
				addFilter.addItem(allFiltersNamesHuman.get(i), allFiltersNames.get(i));
			}
		}
	}
	
//	private QueryWidget createQueryWidget() {
//		ru.rchervot.sanasar.client.Constants constants = 
//			(ru.rchervot.sanasar.client.Constants)GWT.create(ru.rchervot.sanasar.client.Constants.class);
//		
//		ModelSearchCandidate mainModel = new ModelSearchCandidate(constants);
//		
//		ArrayList<Filter.Name> filterNamesCand = new ArrayList<Filter.Name>();
//
//		filterNamesCand.add(Filter.Name.NAME);
//		filterNamesCand.add(Filter.Name.CATEGORIES);
//		filterNamesCand.add(Filter.Name.FTS);
//		filterNamesCand.add(Filter.Name.CAND_ACTIONS);
//		
//		ContentSearchCandidate content = new ContentSearchCandidate(mainModel, constants, 
//				(ConstantsCategories)GWT.create(ConstantsCategories.class), 
//				DefaultQuery.build(new ArrayList<Filter>(), null, Query.JOIN_OPERATOR.AND, 10, 10),
//				DefaultResultsPage.buildEmptyPage(), 
//				filterNamesCand);
//		
//		ControllerSearchCandidate controller = new ControllerSearchCandidate(mainModel, 
//				new HeaderSearchCandidate(mainModel),
//				content,
//				new SectionsSearchCandidate(mainModel),
//				null, null);
//		
//		
//		
//		QueryActionsWidget qaw = new QueryActionsWidget(content, controller);
//		QueryWidget q = new QueryWidget(content, controller, qaw);
//		
//		q.getFilterAdd();
//		
//		addFilterPanelR.add(filtersLabel);
//		addFilterPanelR.add(q.getFilterAdd()); 
//		
//		return q;
//	}
	
	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	@Override
	public HasClickHandlers getCloseButton() {
		return closeButton;
	}

	@Override
	public HasClickHandlers getSearchButton() {
		return searchButton;
	}
	
	@Override
	public HasChangeHandlers getAddFilterListBox() {
		return addFilter;
	}
	
	@Override
	public String getSelectedFilterName() {
		return addFilter.getValue(addFilter.getSelectedIndex());
	}


}
